package com.example.repository;

import com.example.data.approval.Approval;
import com.example.data.approval.ApprovalDO;
import com.example.mappers.ApprovalRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ApprovalRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //获取所有审批信息


    public List<Approval> getAllApproval(){
        String sql = "SELECT * FROM approval ORDER BY empId";
        List<Approval> approvalList = jdbcTemplate.query(sql, new ApprovalRowMapper());
        return approvalList;
    }
    //获取某条审批信息
    //若empID为null，appID不为null，根据appID查询
    //若empID不为null，appID为null，根据empID查询
    //若都不为null，根据appID与empID查询
    //可用SQL ifnull函数
    //由于ifnull函数都是出现在列上，没有出现在where里的不符合格式的不可以插入数据库
    //todo
    public Approval getApprovalByID(Long empID, Long appID){
        String sql = "SELECT * FROM approval WHERE empId=? OR appId=?";//ifnull(name,'no name')
        Approval approval = jdbcTemplate.queryForObject(sql, new ApprovalRowMapper(), empID,appID);
        return approval;
    }
    public Approval addApproval(Approval approval){
        String sql = "INSERT INTO approval(empId,time,appId,approvalType,authorize,comment) VALUES(?, ?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, approval.getEmpID(), approval.getTime(),approval.getAppID(),
                approval.getType(),approval.getApproval(),approval.getComment());
        String sql2 = "SELECT * FROM approval WHERE empId=? ";
        Approval approval2 = jdbcTemplate.queryForObject(sql, new ApprovalRowMapper(), approval.getEmpID());
        return approval2;
    }

    public Approval editApproval(long appID,Approval approval){
        String sql = "UPDATE approval SET empId = ?,time = ?,approvalType = ?,authorize = ?,comment = ? WHERE appId=?";
        jdbcTemplate.update(sql,approval.getEmpID(), approval.getTime(),
                approval.getType(),approval.getApproval(),approval.getComment() ,appID);
        return getApprovalByID(approval.getEmpID(),appID);
    }
    public void deleteApproval(long appID){
        String sql = "DELETE FROM approval WHERE appId=?";
        jdbcTemplate.update(sql, appID);
    }
    public Map<String, Object> queryByAppID(long AppID){
        Map<String, Object> approvalMap = new LinkedHashMap<>();
        approvalMap.put("empID", 11L);
        approvalMap.put("appID", 22L);
        approvalMap.put("comment", "哦哦");
        approvalMap.put("type", 1);
        approvalMap.put("approval", false);
        return approvalMap;
    }
    public List<Approval> getFakeDO(){
        Approval approvalInfo = new ApprovalDO();
        approvalInfo.setEmpID(1L);
        approvalInfo.setAppID(2L);
        approvalInfo.setComment("哦");
        approvalInfo.setType(1);
        approvalInfo.setApproval(true);
        return Arrays.asList(approvalInfo);
    }
}
